package nccloud.web.ct.kjkm_new_s.kjkm_new_smaster.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
//import java.util.List;
import java.util.Map;
//import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.web.codeplatform.framework.action.base.BaseAction;
import nccloud.web.codeplatform.framework.action.base.RequstParamWapper;
import nccloud.web.codeplatform.framework.action.base.RequestDTO;
import nccloud.web.codeplatform.framework.action.base.VOTransform;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.core.util.ArrayUtils;
import nccloud.framework.web.container.IRequest;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.json.core.exception.BusinessException;
import nc.itf.ct.kjkm_new_s.kjkm_new_smaster.IKjkm_new_sMasterVOService;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.nct.kjkm_new_s.Kjkm_new_sMasterVO;

/**
 * 保存action（主子、单表、单form）
 */
public class SaveKjkm_new_sMasterVOAction extends BaseAction{

	@Override
	public Object doAction(IRequest request, RequstParamWapper paramWapper) throws Throwable{
		RequestDTO param = VOTransform.toVO(paramWapper.requestString,RequestDTO.class);
		
		IKjkm_new_sMasterVOService service = ServiceLocator.find(IKjkm_new_sMasterVOService.class);

		Kjkm_new_sMasterVO[] vos = this.getVOs(param, Kjkm_new_sMasterVO.class);
		List<String> pklst = new ArrayList();
		
		String delesql = "( pk_kjkm_new_smaster not in (";
		//本次保存vo集合内校验
		for(int y = 0;y<vos.length;y++) {//外循环
			Kjkm_new_sMasterVO wvo = vos[y];
			for(int u = y+1;u<vos.length;u++) {//内循环
				Kjkm_new_sMasterVO nvo = vos[u];
				if(wvo.getStatus()==3||nvo.getStatus()==3) {
					continue;
				}
				if((wvo.getAttributeValue("def1")+"").equals(nvo.getAttributeValue("def1")+"")&&
						(wvo.getAttributeValue("def2")+"").equals(nvo.getAttributeValue("def2")+"")&&
						(wvo.getAttributeValue("def3")+"").equals(nvo.getAttributeValue("def3")+"")&&
						(wvo.getAttributeValue("def4")+"").equals(nvo.getAttributeValue("def4")+"")) {
					String map4  = (String) new BaseDAO().executeQuery("select name from org_orgs where pk_org = '"+nvo.getAttributeValue("def4")+""+"'", new ColumnProcessor());
					String map1  = (String) new BaseDAO().executeQuery("select dispname from bd_accasoa WHERE pk_accasoa = '"+nvo.getAttributeValue("def1")+""+"'", new ColumnProcessor());
					String map2  = (String) new BaseDAO().executeQuery("select eps_name FROM pm_eps where pk_eps = '"+nvo.getAttributeValue("def2")+""+"'", new ColumnProcessor());
					String map3  = (String) new BaseDAO().executeQuery("select name from bd_defdoc where pk_defdoc = '"+nvo.getAttributeValue("def3")+""+"'", new ColumnProcessor());
					throw new BusinessException(map4+"+"+map1+"+"+map2+"+"+map3+",数据重复。");//抛出异常
				}
			}
			delesql = delesql+"'"+wvo.getPk_kjkm_new_smaster()+"',";
			if((y%800 == 0) && y !=0) {
				delesql = delesql.substring(0, delesql.length()-1)+") and pk_kjkm_new_smaster not in (";
			}
		}
		//end
		//查询当前保存数据外数据库集合
		String[] allpks = service.listKjkm_new_sMasterVOPkByCond(delesql.substring(0, delesql.length()-1)+") )");
		//查询当前分页数据
		Kjkm_new_sMasterVO[] sjkvos =  service.listKjkm_new_sMasterVOByPk(allpks,true);
		//数据库集合校验
		for(int y = 0;y<vos.length;y++) {
			Kjkm_new_sMasterVO wvo = vos[y];
			if(sjkvos!=null) {
			for(int r = 0;r<sjkvos.length;r++) {
				Kjkm_new_sMasterVO nvo = sjkvos[r];
				if(wvo.getStatus()==3||nvo.getStatus()==3) {
					continue;
				}
				if((wvo.getAttributeValue("def1")+"").equals(nvo.getAttributeValue("def1")+"")&&
						(wvo.getAttributeValue("def2")+"").equals(nvo.getAttributeValue("def2")+"")&&
						(wvo.getAttributeValue("def3")+"").equals(nvo.getAttributeValue("def3")+"")&&
						(wvo.getAttributeValue("def4")+"").equals(nvo.getAttributeValue("def4")+"")) {
					String map4  = (String) new BaseDAO().executeQuery("select name from org_orgs where pk_org = '"+nvo.getAttributeValue("def4")+""+"'", new ColumnProcessor());
					String map1  = (String) new BaseDAO().executeQuery("select dispname from bd_accasoa WHERE pk_accasoa = '"+nvo.getAttributeValue("def1")+""+"'", new ColumnProcessor());
					String map2  = (String) new BaseDAO().executeQuery("select eps_name FROM pm_eps where pk_eps = '"+nvo.getAttributeValue("def2")+""+"'", new ColumnProcessor());
					String map3  = (String) new BaseDAO().executeQuery("select name from bd_defdoc where pk_defdoc = '"+nvo.getAttributeValue("def3")+""+"'", new ColumnProcessor());
					throw new BusinessException(map4+"+"+map1+"+"+map2+"+"+map3+",数据重复。");//抛出异常
				}
			}
			}
		}
		
		
		vos = service.saveKjkm_new_sMasterVO(vos);
		Object result = buildResult(param,param.getBillCard().getHead()!=null,null,vos);
		return result;
	
	}
	
}